//
//  VibrationSetViewController.m
//  Lightthings
//
//  Created by 王岩 on 2024/11/29.
//

#import "VibrationSetViewController.h"

@interface VibrationSetViewController ()
@property (weak, nonatomic) IBOutlet UIButton *itemBtn1;
@property (weak, nonatomic) IBOutlet UIButton *itemBtn2;
@property (weak, nonatomic) IBOutlet UIButton *itemBtn3;



@property (weak, nonatomic) IBOutlet UILabel *timesLab;

@end

@implementation VibrationSetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.jy_title = SWString(@"振动设置");
    
    NSString * uuid = self.metaInfo[@"uuid"];
    NSArray * list = [FRQBluetoothManager shareManager].curPeripherals;
    for (CBPeripheral * per in list) {
        if ([per.identifier.UUIDString isEqualToString:uuid]) {
            self.peripheral = per;
        }
    }
    
    [self.itemBtn1 setTitle:SWString(@"弱") forState:0];
    [self.itemBtn2 setTitle:SWString(@"中") forState:0];
    [self.itemBtn3 setTitle:SWString(@"强") forState:0];
    
    [self.itemBtn1 setIconInLeftWithSpacing:10];
    [self.itemBtn2 setIconInLeftWithSpacing:10];
    [self.itemBtn3 setIconInLeftWithSpacing:10];
    
    self.itemBtn1.selected = YES;
    
    self.timesLab.zh_action = kMsgForward(@"showTimesView", WKProxy(self));
    
    [SWInitializer jy_initRedButtonWithTarget:self action:@selector(clickSureBtnAction) frame:CGRectMake(0, 0, kScreenWidth - 100, 50) title:SWString(@"Confirm") on:self.view layout:^(MASConstraintMaker * _Nonnull make) {
        kMakeLV(50);
        kMakeBV(-100);
        kMakeWHV(kScreenWidth-100, 50);
    }];
    
    [self readData];
    // Do any additional setup after loading the view from its nib.
}

- (void)readData {
    [[WatchCommandTool sharedInstance] readStormCommand:Command_VibrationSet ToPeripheral:self.peripheral reBack:^(NSString * _Nonnull backData) {
        if (backData.length) {
            NSString * valueStr = [backData substringWithRange:NSMakeRange(26, 2)];
            unsigned long value = strtoll([[NSString stringWithFormat:@"0x%@",valueStr] UTF8String], 0, 16);
            self.itemBtn1.selected = value == 1;
            self.itemBtn2.selected = value == 2;
            self.itemBtn3.selected = value == 3;
            NSLog(@"开关数值：%lu",value);
            
            valueStr = [backData substringWithRange:NSMakeRange(28, 2)];
            value = strtoll([[NSString stringWithFormat:@"0x%@",valueStr] UTF8String], 0, 16);
            NSLog(@"次数：%lu",value);
            self.timesLab.text = [NSString stringWithFormat:@"%ld",value];
            
        }
    }];
}

- (IBAction)clickBtn:(UIButton *)sender {
    
    self.itemBtn1.selected = sender.tag == 100;
    self.itemBtn2.selected = sender.tag == 200;
    self.itemBtn3.selected = sender.tag == 300;
    
}

- (void)showTimesView {
    
    NSArray * list = @[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10"];
    
    [BRStringPickerView showPickerWithTitle:nil dataSourceArr:list selectIndex:5 resultBlock:^(BRResultModel * _Nullable resultModel) {
        self.timesLab.text = resultModel.value;
    }];
    
}

- (void)clickSureBtnAction {
    
    int a = self.itemBtn2.selected?2:(self.itemBtn3.selected?3:1);
    
    NSMutableArray * arr = [[NSMutableArray alloc] init];
    unsigned char byte0 = (a & 0xff);
    arr[0] = @(byte0);
    unsigned char byte1 = (self.timesLab.text.intValue & 0xff);
    arr[1] = @(byte1);

    [[WatchCommandTool sharedInstance] writeStormCommand:Command_VibrationSet ToPeripheral:self.peripheral Value:arr reBack:^(NSString * _Nonnull backData) {
        if (backData) {
            if ([backData hasSuffix:@"00"]) {
                toast(SWString(@"Success"));
            }else {
                toast(SWString(@""));
            }
//            [self.navigationController popViewControllerAnimated:YES];
        }
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
